<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Admin;

use think\Request;

/**
 * 私信后台控制器
 */
class PrivateMessageController extends BaseController
{
    /**
     * 默认入口方法（兼容资源路由）
     */
    public function index()
    {
        return $this->getList(request());
    }
    
    /**
     * 获取私信列表
     */
    public function getList(Request $request)
    {
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        $fromUserId = $request->param('from_user_id/d', 0);
        $toUserId = $request->param('to_user_id/d', 0);
        $isRead = $request->param('is_read', '');
        $content = $request->param('content', '');
        $startTime = $request->param('start_time', '');
        $endTime = $request->param('end_time', '');
        
        $params = [];
        if ($fromUserId > 0) {
            $params['from_user_id'] = $fromUserId;
        }
        if ($toUserId > 0) {
            $params['to_user_id'] = $toUserId;
        }
        if ($isRead !== '') {
            $params['is_read'] = $isRead;
        }
        if (!empty($content)) {
            $params['content'] = $content;
        }
        if (!empty($startTime)) {
            $params['start_time'] = $startTime;
        }
        if (!empty($endTime)) {
            $params['end_time'] = $endTime;
        }
        
        $result = $this->privateMessageModel->getList($params, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取私信详情
     */
    public function getDetail(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $detail = $this->privateMessageModel->getDetail($id);
        if (!$detail) {
            return $this->error('私信不存在');
        }
        
        return $this->success('获取成功', $detail);
    }
    
    /**
     * 发送系统私信给用户
     */
    public function sendSystemMessage(Request $request)
    {
        $toUserId = $request->param('to_user_id/d', 0);
        $content = $request->param('content', '');
        
        if ($toUserId <= 0) {
            return $this->error('接收者ID不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $result = $this->privateMessageModel->sendSystemMessage($toUserId, $content);
        if (!$result) {
            return $this->error('发送失败');
        }
        
        return $this->success('发送成功', ['id' => $result]);
    }
    
    /**
     * 批量发送系统私信
     */
    public function batchSendSystemMessage(Request $request)
    {
        $userIds = $request->param('user_ids');
        $content = $request->param('content', '');
        
        if (empty($userIds)) {
            return $this->error('接收者ID不能为空');
        }
        
        if (!is_array($userIds)) {
            $userIds = explode(',', $userIds);
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $successCount = 0;
        foreach ($userIds as $userId) {
            $result = $this->privateMessageModel->sendSystemMessage((int)$userId, $content);
            if ($result) {
                $successCount++;
            }
        }
        
        return $this->success('发送成功', ['success_count' => $successCount, 'total_count' => count($userIds)]);
    }
    
    /**
     * 删除私信
     */
    public function delete(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        // 管理员可以删除任何私信，不需要验证用户ID
        $entity = $this->privateMessageModel->getDetail($id);
        if (!$entity) {
            return $this->error('私信不存在');
        }
        
        $result = $entity->delete();
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 批量删除私信
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        // 管理员可以批量删除私信，不需要验证用户ID
        $result = $this->privateMessageModel->batchDelete($ids, 0);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
}
